package com.orifine.sql.simple;


public class SimpleSqlBuilder {
	private static final String AND = ") \nAND (";
	private static final String OR = ") \nOR (";

	private SimpleSql sql;

	public SimpleSqlBuilder() {
		sql = new SimpleSql();
	}

	public SimpleSqlBuilder update(String table) {
		sql.statementType = SimpleSql.StatementType.UPDATE;
		sql.tables.add(table);
		return this;
	}

	public SimpleSqlBuilder set(String sets) {
		sql.sets.add(sets);
		return this;
	}

	public String toString() {
		return sql.sql();
	}

	public  SimpleSqlBuilder insertInto(String tableName) {
		sql.statementType = SimpleSql.StatementType.INSERT;
		sql.tables.add(tableName);
		return this;
	}

	public  SimpleSqlBuilder value(String columns, String values) {
		sql.columns.add(columns);
		sql.values.add(values);
		return this;
	}

	public  SimpleSqlBuilder select(String columns) {
		sql.statementType = SimpleSql.StatementType.SELECT;
		sql.select.add(columns);
		return this;
	}

	public  SimpleSqlBuilder distinct() {
		sql.distinct = true;
		return this;
	}

	public  SimpleSqlBuilder deleteFrom(String table) {
		sql.statementType = SimpleSql.StatementType.DELETE;
		sql.tables.add(table);
		return this;
	}

	public  SimpleSqlBuilder from(String table) {
		sql.tables.add(table);
		return this;
	}

	public  SimpleSqlBuilder join(String join) {
		sql.join.add(join);
		return this;
	}

	public  SimpleSqlBuilder innerJoin(String join) {
		sql.innerJoin.add(join);
		return this;
	}

	public  SimpleSqlBuilder leftOuterJoin(String join) {
		sql.leftOuterJoin.add(join);
		return this;
	}

	public  SimpleSqlBuilder rightOuterJoin(String join) {
		sql.rightOuterJoin.add(join);
		return this;
	}

	public  SimpleSqlBuilder outerJoin(String join) {
		sql.outerJoin.add(join);
		return this;
	}

	public  SimpleSqlBuilder where(String conditions) {
		sql.where.add(conditions);
		sql.lastList = sql.where;
		return this;
	}

	public  SimpleSqlBuilder or() {
		sql.lastList.add(OR);
		return this;
	}

	public  SimpleSqlBuilder and() {
		sql.lastList.add(AND);
		return this;
	}

	public  SimpleSqlBuilder groupBy(String columns) {
		sql.groupBy.add(columns);
		return this;
	}

	public  SimpleSqlBuilder having(String conditions) {
		sql.having.add(conditions);
		sql.lastList = sql.having;
		return this;
	}

	public  SimpleSqlBuilder orderBy(String columns) {
		sql.orderBy.add(columns);
		return this;
	}
	
	public SimpleSqlBuilder limit(Integer offset, Integer rows) {
		sql.offset = offset;
		sql.rows = rows;
		return this;
	}
}
